/* 
 * Created on 2007-1-16
 * Last modified on 2011-7-24
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.commons.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sourceforge.pinyin4j.PinyinHelper;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import sun.misc.BASE64Encoder;

public class StringHelper {

	private static Logger logger = Logger.getLogger(StringHelper.class);

	public static final String EMPTY = "";
	public static final String SPACE = "";

	public static void main(String args[]) {
		/*String URI = "/group/topic/g16t30363p3453.html";
		System.out.println(StringHelper.getReplacement(URI,
				"/group/topic/g([0-9]*)t([0-9]*)p([0-9]*)", "$3", null));

		System.out.println("----------------------------------------------");

		
		System.out.println(StringHelper.ubbPattern(URI,
				"/group/topic/g([0-9]*)t([0-9]*)p([0-9]*).html", "$1", null));*/
		//System.out.println(StringHelper.toPinyin("长虹", null));
		String s[] = PinyinHelper.toHanyuPinyinStringArray("长".charAt(0));
		System.out.println(s[0]);
		System.out.println(s[1]);
		
		/*System.out.println(StringHelper.ubbPattern("[fz12]fafdasfdsafdsafsd","\\[fz(.*?)\\]", "$1", null));
		System.out.println(StringHelper.getReplacement("                [fz12]发达电器成销售单据".trim(),"\\[fz(.*?)\\]", "$1", null));
		
		System.out.println(StringHelper.replaces("[fz12]dsafadsfdsaf","\\[fz(.*?)\\]", ""));
		
		System.out.println("[fz12]dsafadsfdsaf".replaceAll("\\[fz(.*?)\\]", ""));*/
		
		//String text = "           [ph=10]$card.getGoodsName()[/ph][ph=20]发达电器[/ph][ph=18]何建兴,王志强[/ph]";
		//String text = "dsafsafsafsafs[ph=20]发达电器[/ph]afsafd]";
		//StringHelper sh = new StringHelper();
		//System.out.println(sh.formatText(text));
		
		System.out.println(getInitial("格力/GREE",1));
		System.out.println(getInitial("长虹电视C5000",1,null));
	}
	
	
	public String formatText(String text) {
		StringBuilder sb = new StringBuilder();
		String[] txts = text.split("\\[\\/ph\\]");
		for(String t : txts) {
			System.out.println(t);
			if(t.indexOf("[ph") != -1) {
				sb.append(_formatText(t+"[/ph]"));
			}
			else {
				sb.append(t);
			}
		}
		return sb.toString();
	}

	/**
	 * 为控制模板位置需要设计占位符标签 placeholder
	 * [ph=10][/ph]表示10个占位符
	 * [ph=10]ABC[/ph] 打印ABC后面跟7个空格
	 */
	private String _formatText(String text) {
		String _ph = StringHelper.getReplacement(text.trim(),"(.*?)\\[ph=(.*?)\\](.*?)\\[\\/ph\\]", "$2", null);
		String s = StringHelper.getReplacement(text.trim(),"(.*?)\\[ph=(.*?)\\](.*?)\\[\\/ph\\]", "$3", null);
		if(s == null || _ph == null) {
			return text;
		}
		int ph = TypeChange.stringToInt(_ph);
		int n = ph - s.length(); // 规定长度减去字符长度
		String str = text;
		if(n > 0) {
			StringBuilder sb = new StringBuilder();
			for(int i=0; i<n; i++) {
				sb.append(" ");
			}
			str = StringHelper.ubbPattern(text, "\\[ph=(.*?)\\](.*?)\\[\\/ph\\]", "$2"+sb.toString(), s);
		}
		else {
			str = StringHelper.ubbPattern(text, "\\[ph=(.*?)\\](.*?)\\[\\/ph\\]", "$2", s);
		}
		return str;
	}

	/**
	 * 替换指定匹配的字符，如果没有匹配内容则返回定义的默认返回字符串
	 * 
	 * @param input
	 * @param regex
	 * @param replacement
	 * @param defaultStr
	 * @return
	 */
	public static final String getReplacement(String input, String regex,
			String replacement, String defaultStr) {
		if (ValidatorHelper.isEmpty(input)) {
			return defaultStr;
		}
		try {
			Pattern pattern = Pattern.compile(regex, 2); // 参数2表示大小写不区分
			Matcher matcher = pattern.matcher(input);
			if (matcher.find()) {
				StringBuffer sb = new StringBuffer();
				matcher.appendReplacement(sb, replacement);
				return sb.toString();
			}
		} catch (Exception e) {
			logger.error(e.toString());
		}
		return defaultStr;
	}

	/**
	 * 比较两个字符串是否相等
	 * 
	 * @param str1
	 * @param str2
	 * @return
	 */
	public static final boolean equalsIgnoreCase(String str1, String str2) {
		if (str1 == null && str2 == null) {
			return true;
		} else if (str1 == null && str2 != null) {
			return false;
		} else if (str1 != null && str2 == null) {
			return false;
		}
		return str1.equalsIgnoreCase(str2);
	}

	public static final boolean isUTF8Query(String q) {
		byte[] bytes;
		try {
			bytes = q.getBytes("ISO-8859-1");
			for (int i = 0; i < bytes.length; i++) {
				// java 中的byte是有符号的，大于127的都为负数。先转换为int型正数再比较。
				int first = 0x100 + bytes[i];
				// 寻找查询关键词中的第一个中文字符的第一个字节
				if (first < 0xE0 || first > 0xEF) {
					continue;
				}
				if (i + 2 < bytes.length) {
					int second = 0x100 + bytes[i + 1];
					int third = 0x100 + bytes[i + 2];
					if (second >= 0x80 && second <= 0xBF && third >= 0x80
							&& third <= 0xBF) {
						return true;
					}
				}
			}
		} catch (UnsupportedEncodingException e) {
			logger.error(e);
		}
		return false;
	}
	
	/**
	 * 获取单词拼音每个字的首字母
	 * @param name
	 * @param index 多音字选第几个
	 * @return
	 */
	public static final String getInitial(String name, int index) {
		return getInitial(name, index, null);
	}
	
	public static final String getInitial(String name, String def) {
		return getInitial(name, 0, def);
	}

	/**
	 * 
	 * @param name
	 * @return String
	 */
	public static final String getInitial(String name,int index, String def) {
		if (ValidatorHelper.isEmpty(name)) {
			return def;
		}
		StringBuilder sb = new StringBuilder();
		/*name = name.replace("'", " ").replace("*", " ").replace("&", " ").replace("-", " ");
		if (name.indexOf(" ") != -1) {
			String[] strs = name.split(" ");
			for (String tmp : strs) {
				if (Utils.isLetter(tmp)) {
					sb.append(StringHelper.substring(tmp, 0, 1, EMPTY).toLowerCase());
				}
			}
			if (sb.length() > 0) {
				return sb.toString();
			}
		}*/
		for (int i = 0; i < name.length(); i++) {
			String temp = name.substring(i, i + 1);
			if (Utils.isNumber(temp)) {
				sb.append(SPACE);
				continue;
			} 
			char c = name.charAt(i);
			String[] str = PinyinHelper.toHanyuPinyinStringArray(c);
			if (ValidatorHelper.isEmpty(str)) {
				sb.append(SPACE);
				continue;
			} 
			if(str.length<(index+1)) {
				index = 0;
			}
			if (!ValidatorHelper.isEmpty(str)) {
				sb.append(str[index].replace(":", EMPTY).substring(0, 1));
			} else {
				sb.append(EMPTY);
			}
		}
		if (logger.isDebugEnabled()) {
			logger.debug("name = " + name + ", initial = " + sb.toString());
		}
		return sb.toString();
	}

	public static final String toPinyin(String name, String def) {
		if (ValidatorHelper.isEmpty(name)) {
			return def;
		}
		return toPinyin(name);
	}

	/**
	 * 将汉字转化成拼音,并将每个汉字的首字母大写
	 * 
	 * @param name
	 * @return
	 */
	public static final String toPinyin(String name) {
		return toPinyin(name, 0, null);
	}
	
	
	public static final String toPinyin(String name, int index) {
		return toPinyin(name, index, null);
	}
	
	
	/**
	 * 
	 * @param name
	 * @param index 多音字选第几个
	 * @param def
	 * @return
	 */
	public static final String toPinyin(String name, int index, String def) {
		if (Utils.isLetter(name.replace(" ", EMPTY))) {
			return StringHelper.initialToUpperCase(name.replace(" ", EMPTY));
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < name.length(); i++) {
			String temp = name.substring(i, i + 1);
			if (Utils.isLetter(temp) || Utils.isNumber(temp)) {
				sb.append(temp);
				continue;
				// 如果是字母或者是数字则累计字符串后跳回继续循环
			} 
			String[] str = PinyinHelper.toHanyuPinyinStringArray(name.charAt(i));
			//如果取不到拼音则用空格来连接
			if (ValidatorHelper.isEmpty(str)) {
				sb.append(SPACE);
				continue;
			} 
			if(str.length<(index+1)) {
				index = 0;
			}
			String tmp = str[index].replace(":", EMPTY);
			String pinyin = StringHelper.initialToUpperCase(tmp.substring(0, tmp.length() - 1));
			if (!ValidatorHelper.isEmpty(pinyin)) {
				sb.append(pinyin);
			} else {
				sb.append(EMPTY);
			}
		}
		return sb.toString();
	}


	/**
	 * 首字母变为大写
	 * 
	 * @param str
	 * @return
	 */
	public static final String initialToUpperCase(String str) {
		if(ValidatorHelper.isNotEmpty(str)) {
			if (Utils.isLetter(str)) {
				if(str.length() >= 1) {
					return str.substring(0, 1).toUpperCase() + str.substring(1);
				}
			} 
		}
		return str;
	}

	public static final String regexUtils(String original) {
		StringBuilder buffer = new StringBuilder(original.length());
		for (int i = 0; i < original.length(); i++) {
			char c = original.charAt(i);
			if (c == '\\' || c == '$') {
				buffer.append("\\").append(c);
			} else {
				buffer.append(c);
			}
		}
		return buffer.toString();
	}

	public static final String asciiToString(String str) {
		List<String> list = StringHelper.matcherList(str, "&#([0-9A-z]*);");
		if (com.yeqiangwei.commons.util.ValidatorHelper.isEmpty(list)) {
			return str;
		}
		for (int i = 0; i < list.size(); i++) {
			String ascii = list.get(i);
			String temp = ascii.substring(2, ascii.length() - 1);
			char c = (char) TypeChange.stringToInt(temp);
			str = str.replace(ascii, String.valueOf(c));
		}
		return str;
	}

	/**
	 * codeType 加密方式， txt加密字符串
	 * 
	 * @deprecated
	 */
	public static final String encodeString(String type, String str) {
		return encrypt(type, str);
	}

	public static final String MD5(String str) {
		return encrypt(str, "md5of32");
	}

	public static final String MD5_16(String str) {
		return encrypt(str, "md5of16");
	}

	private static String encrypt(String str, String type) {
		if (str == null)
			return null;
		if (ValidatorHelper.isEmpty(type) || type.equalsIgnoreCase("md5of16")) {
			MD5 m = new MD5();
			return m.getMD5ofStr16(str);
		} else if (type.equalsIgnoreCase("md5of32")) {
			MD5 m = new MD5();
			return m.getMD5ofStr(str);
		} else if (type.equalsIgnoreCase("md5")) {
			MD5 m = new MD5();
			return m.getMD5ofStr(str);
		} else {
			try {
				MessageDigest gv = MessageDigest.getInstance(type);
				gv.update(str.getBytes());
				return new BASE64Encoder().encode(gv.digest());
			} catch (java.security.NoSuchAlgorithmException e) {
				logger.error(e.toString());
				return null;
			}
		}
	}

	public static final List<String> stringToList(String str, String token) {
		List<String> list = new ArrayList<String>();
		if (ValidatorHelper.isEmpty(str) || ValidatorHelper.isEmpty(token)) {
			return null;
		} else {
			if (str.endsWith(token)) {
				str = str.substring(0, str.length() - token.length());
			}
			StringTokenizer st = new StringTokenizer(str, token);
			for (int i = 0; st.hasMoreTokens(); i++) {
				list.add(st.nextToken().trim());
			}
			return list;
		}
	}

	public static final List<Integer> stringToIntegerList(String str,
			String token) {
		List<Integer> list = new ArrayList<Integer>();
		if (ValidatorHelper.isEmpty(str) || ValidatorHelper.isEmpty(token)) {
			return null;
		} else {
			if (str.endsWith(token)) {
				str = str.substring(0, str.length() - token.length());
			}
			StringTokenizer st = new StringTokenizer(str, token);
			for (int i = 0; st.hasMoreTokens(); i++) {
				list.add(TypeChange.stringToInt(st.nextToken().trim()));
			}
			return list;
		}
	}

	public static final String[] stringToStrings(String str, String token) {
		if (ValidatorHelper.isEmpty(str) || ValidatorHelper.isEmpty(token)) {
			return null;
		} else {
			if (str.endsWith(token)) {
				str = str.substring(0, str.length() - token.length());
			}
			StringTokenizer st = new StringTokenizer(str, token);
			String[] text = new String[st.countTokens()];
			for (int i = 0; st.hasMoreTokens(); i++) {
				text[i] = (st.nextToken().trim());
			}
			return text;
		}
	}

	public static final int length(String str) {
		if (ValidatorHelper.isEmpty(str)) {
			return 0;
		} else {
			return str.length();
		}
	}

	public static final int Glength(String str) {
		if (ValidatorHelper.isEmpty(str)) {
			return 0;
		} else {
			return str.length();
		}
	}

	/**
	 * 
	 * @param res
	 * @param s
	 * @param e
	 * @return
	 */
	public static final String substring(String str, int s, int e,
			String defaultStr) {
		if (ValidatorHelper.isEmpty(str) || e > str.length()) {
			return defaultStr;
		}
		return StringUtils.substring(str, s, e);
	}

	public static final String substring(String res, int s, String defaultStr) {
		if (ValidatorHelper.isEmpty(res)) {
			return defaultStr;
		}
		int len = res.length();
		if (len < s)
			s = 0;
		if (s < 0)
			s = 0;
		return res.substring(s);
	}

	/**
	 * 
	 * 中英文混和字符串按特定长度截取
	 * 
	 * @param res
	 *            中英文混和字符。
	 * @param start
	 *            截取起始位置。
	 * @param length
	 *            截取长度。
	 * @param charSet
	 *            字符编码
	 * @return default null
	 */
	public static final String Gsubstring(String res, int start, int length,
			String charSet) {
		if (ValidatorHelper.isEmpty(charSet)) {
			charSet = Constants.UTF_8;
		}
		if (ValidatorHelper.isEmpty(res)) {
			return null;
		}
		if (res.length() < length) {
			return res;
		}
		res = res.trim(); // 去掉空格
		if (start < 1) {
			start = 1;
		}
		int i_Start, i_Len, i, j, ilenByte, i_S, i_L;
		i_Start = 0;
		i_S = 0;
		i_Len = 0;
		i_L = 0;
		try {
			byte[] resBytes = res.getBytes(charSet);
			ilenByte = resBytes.length;
			for (i = 0; i < ilenByte; i++) {
				i_Start = i_Start + 1;
				if (i_Start <= start) {
					i_S = i_S + 1;
				}

				if (i_Start >= start) {
					i_Len = i_Len + 1;
					i_L = i_L + 1;
				}

				if (resBytes[i] < 0) {
					i = i + 2;
					i_Start = i_Start + 1;
					if (i_Start < start) {
						i_S = i_S + 2;
					}
					if (i_Start >= start) {
						i_Len = i_Len + 1;
						i_L = i_L + 2;
					}
				}
				if (i_Len >= length)
					i = ilenByte;
			}
			byte[] dest = new byte[i_L];
			for (j = 0; j < i_L; j++) {
				dest[j] = resBytes[i_S + j - 1];
			}
			return new String(dest, charSet);
		} catch (Exception e) {
			logger.error(e.toString());
		}
		return null;
	}

	public static final String replaces(String src, String fnd, String rep) {
		if (ValidatorHelper.isEmpty(src)) {
			return StringHelper.EMPTY;
		}
		if (ValidatorHelper.isEmpty(fnd)) {
			return StringHelper.EMPTY;
		}
		String dst = src;
		int idx = dst.indexOf(fnd);
		while (idx >= 0) {
			dst = dst.substring(0, idx) + rep
					+ dst.substring(idx + fnd.length(), dst.length());
			idx = dst.indexOf(fnd, idx + rep.length());
		}
		return dst;
	}

	public static final String replace(String text, String oldStr, String newStr) {
		if (!ValidatorHelper.isEmpty(text)) {
			return StringUtils.replace(text, oldStr, newStr, -1);
		}
		return text;
	}

	/**
	 * 转换为XML编码
	 * 
	 * @param src
	 * @return com.yeqiangwei.tools
	 */
	public static final String xmlEncoder(String src) {
		if (ValidatorHelper.isEmpty(src)) {
			return "";
		}
		String xmlencode = src;
		xmlencode = replaces(xmlencode, "&", "&amp;");
		xmlencode = replaces(xmlencode, "<", "&lt;");
		xmlencode = replaces(xmlencode, ">", "&gt;");
		xmlencode = replaces(xmlencode, "\"", "&quot;");
		xmlencode = replaces(xmlencode, "\'", "&acute;");
		return xmlencode;
	}

	public static final String noscript(String html) {
		if (!ValidatorHelper.isEmpty(html)) {
			html = html.replaceAll("<script(.*?)>", "&lt;script&gt;");
			html = html.replaceAll("<(.*?)script>", "&lt;script&gt;");
		}
		return html;
	}

	/**
	 * 用于HTML字符串替换
	 * 
	 * @param src
	 * @return com.yeqiangwei.tools
	 */
	public static final String htmlEncoder(String html) {
		if (!ValidatorHelper.isEmpty(html)) {
			html = html.replaceAll("&(.*?)；", "&$1;");
			html = StringUtils.replace(html, "&hellip;", "…", -1);
			html = StringUtils.replace(html, "&middot;", "·", -1);
			html = StringUtils.replace(html, "&hel", "…", -1);
			html = StringUtils.replace(html, "&ldquo;", "“", -1);
			html = StringUtils.replace(html, "&rdquo;", "”", -1);
			html = StringUtils.replace(html, "&mdash;", "—", -1);
			html = StringUtils.replace(html, "&bull;", "•", -1);
			html = StringUtils.replace(html, "&", "&amp;", -1);
			html = StringUtils.replace(html, "<", "&lt;", -1);
			html = StringUtils.replace(html, ">", "&gt;", -1);
			html = StringUtils.replace(html, "\"", "&quot;", -1);
			html = StringUtils.replace(html, "  ", "&nbsp;", -1);
			html = StringUtils.replace(html, "	", "&nbsp;&nbsp;&nbsp;&nbsp;",
					-1); // TAB
			html = StringUtils.replace(html, "\r\n", "<br/>", -1);
			html = StringUtils.replace(html, "\n", "<br/>", -1);
			html = StringUtils.replace(html, "\r", "<br/>", -1);
		}
		return html;
	}

	public static final String htmlUncoder(String html) {
		if (!ValidatorHelper.isEmpty(html)) {
			html = html.replaceAll("&(.*?)；", "&$1;");
			html = StringHelper.ubbPattern(html, "<br(.*?)>", "\r\n", "")
					.replaceAll("&lt;", "<").replaceAll("&gt;", ">")
					.replaceAll("&quot;", "\"").replaceAll("&nbsp;", " ")
					.replaceAll("&amp;", "&");
			html = StringHelper.asciiToString(html);
		}
		return html;
	}

	/**
	 * 正则匹配并完成替换过程
	 * 
	 * @param str
	 *            要检索的字符串
	 * @param cp
	 *            正则匹配条件字符串
	 * @param mc
	 *            要替换成的字符串
	 * @return
	 */
	public static final String ubbPattern(String str, String cp, String mc,
			String defaultStr) {
		if (ValidatorHelper.isEmpty(str)) {
			return defaultStr;
		}
		try {
			Pattern pattern = Pattern.compile(cp, 2); // 参数2表示大小写不区分
			Matcher matcher = pattern.matcher(str);
			str = matcher.replaceAll(mc);
		} catch (Exception e) {
			logger.error(e.toString());
		}
		return str;
	}

	/**
	 * 循环找出匹配内容
	 * 
	 * @param str
	 * @param cp
	 * @param s
	 *            循环匹配内容间隔符
	 * @param defaultStr
	 * @return
	 */
	public static final String matcherStr(String str, String cp, String s,
			String defaultStr) {
		String txt = defaultStr;
		if (!ValidatorHelper.isEmpty(str)) {
			txt = str;
			Pattern p = Pattern.compile(cp, 2); // 参数2表示大小写不区分
			Matcher m = p.matcher(txt);
			StringBuilder sb = new StringBuilder();
			int i = 0;
			// 使用循环将句子里所有匹配的内容找出并替换再将内容加到sb里
			while (m.find()) {
				i++;
				sb.append(m.group());
				sb.append(s);
			}
			txt = String.valueOf(sb);
		}
		return txt;
	}

	/**
	 * 指定第几次匹配才执行替换
	 * 
	 * @param str
	 * @param cp
	 * @param s
	 * @param loop
	 *            指定第几次, 0表示循环所有匹配内
	 * @param defaultStr
	 *            默认内容
	 * @return
	 */
	public static final String matcherStr(String str, String cp, String s,
			int loop, String defaultStr) {
		if (ValidatorHelper.isEmpty(str)) {
			return defaultStr;
		}
		Pattern p = Pattern.compile(cp, 2); // 参数2表示大小写不区分
		Matcher m = p.matcher(str);
		StringBuilder sb = new StringBuilder();
		int i = 0;
		boolean result = m.find();
		if (!result) {
			return defaultStr;
		}
		// 使用循环将句子里所有匹配的内容找出并替换再将内容加到sb里
		while (result) {
			i++;
			if (loop == i) {
				sb.append(m.group());
				sb.append(s);
			} else if (loop == 0) {
				sb.append(m.group());
				sb.append(s);
				// 继续查找下一个匹配对象
				result = m.find();
			} else {
				// 继续查找下一个匹配对象
				result = m.find();
			}
		}
		return sb.toString();
	}

	/**
	 * 循环找出匹配内容
	 * 
	 * @param str
	 * @param regex
	 * @return
	 */
	public static final List<String> matcherList(String str, String regex) {
		if (ValidatorHelper.isEmpty(str)) {
			return null;
		}
		String txt = str;
		List<String> list = new ArrayList<String>();
		txt = str;
		Pattern p = Pattern.compile(regex, 2); // 参数2表示大小写不区分
		Matcher m = p.matcher(txt);
		// 使用循环将句子里所有匹配的内容找出并替换再将内容加到sb里
		while (m.find()) {
			list.add(m.group());
			// 继续查找下一个匹配对象
		}
		return list;
	}

	/**
	 * 
	 * @param txts
	 *            字符串
	 * @param ii
	 *            取指定位置的字符
	 * @param t
	 *            分割符
	 * @param index
	 *            默认字符串
	 * @return
	 */
	public static final String locator(String txts, int ii, String t,
			String defaultStr) {
		String str = defaultStr;
		if (!ValidatorHelper.isEmpty(txts)) {
			if (txts.substring(0, 1).equals(t)) {
				txts = "~" + txts;
			}
			txts = txts.replace((t + t), (t + "~" + t));
			StringTokenizer st = new StringTokenizer(txts, t);
			String[] txtslist;
			int ct = st.countTokens(); // 4个元素可以循环四次
			if (ii > ct) { // 判断指定数组长度是否越界
				str = defaultStr;
			} else {
				txtslist = new String[ct];
				for (int i = 0; st.hasMoreTokens(); i++) {
					txtslist[i] = (st.nextToken());
					if (i == ii) {
						str = txtslist[ii];
					}
				}
				str = str.replace("~", EMPTY);
			}
		}
		return str;
	}

	public static final Map<Integer, String> stringToHashMap(String str,
			String token) {
		Map<Integer, String> hm = new HashMap<Integer, String>();
		if (!ValidatorHelper.isEmpty(str)) {
			if (str.substring(0, 1).equals(token)) {
				str = "~" + str;
			}
			StringTokenizer st = new StringTokenizer(str, token);
			for (int i = 0; st.hasMoreTokens(); i++) {
				String s = String.valueOf(st.nextToken());
				hm.put(i, s.replace("~", EMPTY));
			}
		}
		return hm;
	}

	public static final Map<String, String> stringToHashMap(String keys,
			String values, String token) {
		Map<String, String> map = new HashMap<String, String>();
		if (!ValidatorHelper.isEmpty(values) && !ValidatorHelper.isEmpty(keys)) {
			List<String> value = StringHelper.stringToList(values, token);
			List<String> key = StringHelper.stringToList(keys, token);
			for (int i = 0; i < key.size(); i++) {
				String k = key.get(i);
				String v = value.get(i);
				if (!ValidatorHelper.isEmpty(k)) {
					map.put(k, v);
				}
			}
		}
		return map;
	}

	public static final String getString(String str, String start, String last,
			String defaultStr) {
		if (ValidatorHelper.isEmpty(str) || ValidatorHelper.isEmpty(start)
				|| ValidatorHelper.isEmpty(last)) {
			return defaultStr;
		} else {
			str = str.replace("\r", "#r").replace("\n", "#n");
			int s = str.indexOf(start);
			if (s == -1) {
				return defaultStr;
			}
			String temp = str.substring(s + start.length());
			int l = temp.indexOf(last);
			if (l == -1) {
				return defaultStr;
			}
			str = temp = temp.substring(0, l);
		}
		return str.replace("#r", "\r").replace("#n", "\n");
	}

	public static final String getString(String str, String start,
			String defaultStr) {
		if (ValidatorHelper.isEmpty(str) || ValidatorHelper.isEmpty(start)) {
			return defaultStr;
		} else {
			str = str.replace("\r", "#r").replace("\n", "#n");
			int s = str.indexOf(start);
			if (s == -1) {
				return defaultStr;
			}
			str = str.substring(s + start.length());
		}
		return str.replace("#r", "\r").replace("#n", "\n");
	}
	
	public static final String toGBK(String s) {
		if (ValidatorHelper.isEmpty(s)) {
			return s;
		}
		try {
			// 编码转化
			s = new String(s.getBytes(), Constants.GBK);
		} catch (UnsupportedEncodingException e) {
			logger.error(e.toString());
		}
		return s;
	}

	public static final String toUTF8(String s) {
		if (ValidatorHelper.isEmpty(s)) {
			return s;
		}
		try {
			// 编码转化
			s = new String(s.getBytes(Constants.ISO_8895_1), Constants.UTF_8);
		} catch (UnsupportedEncodingException e) {
			logger.error(e.toString());
		}
		return s;
	}

	public static final int indexOf(String str, String s) {
		return StringUtils.indexOf(str, s);
	}

	public static final int lastIndexOf(String str, String s) {
		return StringUtils.lastIndexOf(str, s);
	}
}
